<?php
namespace App\Admin\Models;
use Common\BaseModel;
class Articles extends BaseModel
{
    protected $res;
    protected $redis_key="admin_";
    public function initialize()
    {
        parent::initialize();
        $this->setSource('f_articles');
    }

    /**
     * @param $params获取素材列表
     * @return mixed
     */
    public function article_list($params)
    {
        $condition = "title like '%{$params['keywords']}%'";
        if(isset($params['cateid']) && !empty($params['cateid'])){
            $condition .= " AND article_category_id='{$params['cateid']}'";
        }
        $cate_list = $this::find(
        array(
            $condition,
            "order" => 'created_at desc',
            "columns"=>'id,article_category_id,author,title,created_at,images',
        ));
        return $cate_list;
    }


    /**
     * 根据营销id获取营销
     */
    public function find_first_by_Id($id){
        if(empty($id)){
            return '';
        }
        $cate_info = $this->findFirstById($id);
        if($cate_info){
            return $cate_info->toarray();
        }else{
            return [];
        }
    }

    public function save_article($data){
        $data['published']=1;
        $result = $this::save($data);
        return $result;
    }

    /**
     * @param $params获取素材列表
     * @return mixed
     */
    public function article_count($cate_id)
    {
        $condition = "article_category_id={$cate_id}";
        $cate_list = $this::count(
            array(
                $condition,
                "columns"=>'id',
            ));
        return $cate_list;
    }

}
